package com.rcloud.server.sealtalk.controller;

import com.rcloud.server.sealtalk.model.response.APIResult;
import com.rcloud.server.sealtalk.model.response.APIResultWrap;
import com.rcloud.server.sealtalk.service.AttachFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * 文件上传 controller
 *
 * @author lgh
 */
@Api(tags = "文件上传的接口")
@RestController
@RequestMapping("/file")
public class FileController extends BaseController {

    @Autowired
    private AttachFileService attachFileService;

    @ApiOperation("单个文件上传的接口")
    @PostMapping("/upload/element")
    public APIResult<Object> uploadElementFile(@RequestParam("file") MultipartFile file) throws IOException {
        if (file.isEmpty()) {
            return APIResultWrap.ok("");
        }
        String fileName = attachFileService.uploadFile(file.getBytes(), file.getOriginalFilename());
        return APIResultWrap.ok("https://" + sealtalkConfig.getQiniuBucketDomain() + "/" + fileName);
    }
}
